# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openscad
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2021.01
pkgrel=14
pkgdesc="The programmers solid 3D CAD modeller (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang32' 'clang64' 'clangarm64')
license=("spdx:GPL-2.0-or-later")
url="https://www.openscad.org/"
depends=("${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-opencsg"
         "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             #"${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-cgal"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-imagemagick")
source=("https://files.openscad.org/${_realname}-${pkgver}.src.tar.gz"
        '001-openscad-2021-libintl-collisions.patch'
        '002-openscad-2021-remove-print-polyhedron.patch'
        '003-fix-linking-hidapi-clang.patch'
        '004-fix-building-against-cgal5.4.patch::https://github.com/openscad/openscad/commit/abfebc651343909b534ef337aacc7604c99cf0ea.patch'
        '005-fix-building-against-newer-boost.patch::https://github.com/openscad/openscad/commit/08bf69b4115c989fc5671254e0d05735d01bcca5.patch')
sha256sums=('d938c297e7e5f65dbab1461cac472fc60dfeaa4999ea2c19b31a4184f2d70359'
            'f295d41896cc55f6645e080e527ba3d033357bc744b7f7bb01b9271974ee930e'
            '43cebd358d152f7a6dc1fc27294c67c4938b207d6a06f26fd60117e3a3c71412'
            '0606fe5b76f1f95c23c9dfde8d4e66adf36285c141a5cc11da107022eee297fe'
            '836626d08bd8dcce496bd2db0a73ae1911915388c412dc63d831d40ae1aa9206'
            '583783a6ecb2fe7d23b37bc5e29c6c705065748a6993908ff28ad61b10190ce7')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  # https://github.com/openscad/openscad/commit/4c5d19c3
  patch -p1 -i "${srcdir}/001-openscad-2021-libintl-collisions.patch"
  # https://github.com/openscad/openscad/commit/9aa0d7e9
  patch -p1 -i "${srcdir}/002-openscad-2021-remove-print-polyhedron.patch"
  patch -p1 -i "${srcdir}/003-fix-linking-hidapi-clang.patch"
  patch -p1 -i "${srcdir}/004-fix-building-against-cgal5.4.patch"
  patch -p1 -i "${srcdir}/005-fix-building-against-newer-boost.patch"
  # CGAL 5.4 compatibility
  sed -i "s|CGAL_VERSION_NR < 1050401000|CGAL_VERSION_NR < CGAL_VERSION_NUMBER(5, 4, 0)|" src/cgalutils-tess.cc
  #rm -f libraries/MCAD/*.py
  convert "${srcdir}/${_realname}-${pkgver}/icons/${_realname}.png" -resize 128x128\> "${srcdir}/${_realname}-${pkgver}/icons/${_realname}-128.png"
}

build() {
  msg2 "Will be in future"
  # [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  # mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  # MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  # ${MINGW_PREFIX}/bin/cmake.exe \
    # -G"MSYS Makefiles" \
    # -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    # -DCMAKE_BUILD_TYPE=Release \
    # ../${_realname}-${pkgver}
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}

  local -a _extra_config
  if check_option "debug" "y"; then
    _extra_config+=(CONFIG+=debug)
  else
    _extra_config+=(CONFIG+=release)
  fi

  BOOSTDIR=${MINGW_PREFIX} \
  CGALDIR=${MINGW_PREFIX} \
  EIGENDIR=${MINGW_PREFIX} \
  GLEWDIR=${MINGW_PREFIX} \
  OPENCSGDIR=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/qmake \
    "${_extra_config[@]}" \
    PREFIX="${pkgdir}${MINGW_PREFIX}" \
    VERSION="${pkgver}"

  make
  make INSTALL_ROOT= install
  install -Dm644 "icons/${_realname}.xml" "${pkgdir}${MINGW_PREFIX}/share/mime/packages/${_realname}.xml"
  install -Dm644 COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

  # cd build-${MINGW_CHOST}
  # make DESTDIR=${pkgdir} install
  # install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

}
